package com.cc.servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.stream.StreamReader;
import com.cc.entity.Employee;
import com.cc.entity.Page;
import com.cc.service.EmployeeService;
import com.cc.service.impl.EmployeeServiceImpl;
import com.cc.util.DateUtil;
import com.cc.util.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author CC
 * @version 1.0
 * @description: TODO
 * @date 2025/4/18 9:54
 */
@WebServlet("/employee")
// 文件上传 maxFileSize:最大传输的大小
@MultipartConfig(maxFileSize = 1024 * 1024 * 2)
public class EmployeeServlet extends HttpServlet {
    EmployeeService employeeService = new EmployeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //允许所有来源访问 解决跨域问题 Access-Control-Allow-Origin
        resp.addHeader("Access-Control-Allow-Origin", "*");
        //允许访问的方式
        resp.addHeader("Access-Control-Allow-Method", "POST,GET");
        // 允许的头信息
        resp.addHeader("Access-Control-Allow-Headers", "Content-Type");
        //获取请求参数
        String method = req.getParameter("method") == null ? "list" : req.getParameter("method");
        switch (method) {
            case "list":
                list(req, resp);
                break;
            case "edit":
                edit(req, resp);
                break;
            case "detail":
                detail(req, resp);
                break;
            default:
                break;
        }


    }

    //用户详情
    private void detail(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端参数
        String empId = req.getParameter("empId");
        String empName = req.getParameter("empName");
        String departmentId = req.getParameter("departmentId");
        String position = req.getParameter("position");
        String hireDate = req.getParameter("hireDate");
        String phone = req.getParameter("phone");
        //文件接受使用part对象
        //需要增加@MultipartConfig注解
        Part photo = req.getPart("photo");
        //上传文件
        String photoUrl = FileUtil.getImgUrl(req, photo);
        Employee employee = new Employee();
        if (empId == null) {
            employee.setId(0);
        }else {
            employee.setId(Integer.parseInt(empId));
        }

        employee.setEmpName(empName);
        employee.setDepId(Integer.parseInt(departmentId));
        employee.setPosition(position);
        employee.setStartDate(DateUtil.parseDate(hireDate));
        employee.setPhone(phone);
        employee.setImgUrl(photoUrl);
        //保存数据
        int result = employeeService.edit(employee);
        Map<String,String> map = new HashMap<>();
        if (result > 0){
            map.put("code","200");
            map.put("msg","保存成功");
        }else {
            map.put("code","500");
            map.put("msg","保存失败");
        }
        String jsonString = JSON.toJSONString(map);
        resp.getWriter().write(jsonString);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //当前页码
        String pageNum = req.getParameter("pageNum");
        //搜索用户名
        String empName = req.getParameter("empName");
        if (pageNum == null) {
            pageNum = "1";
        }
        Page<Employee> page = new Page<Employee>();
        page.setPageNum(Integer.parseInt(pageNum));
        page = employeeService.getEmployeeList(page, empName);
        //转为json格式 值为null的属性返回""
        String jsonString = JSON.toJSONString(page, "yyyy-MM-dd", JSONWriter.Feature.WriteMapNullValue);

        resp.getWriter().write(jsonString);
    }
}
